/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.gui;

import com.jme3.bullet.BulletAppState;
import de.lessvoid.nifty.builder.TextBuilder;
import de.lessvoid.nifty.controls.RadioButton;
import de.lessvoid.nifty.controls.radiobutton.builder.RadioButtonBuilder;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.screen.Screen;
import etherblocks.engine.network.NetworkConnectionSettings;
import etherblocks.engine.network.NetworkConnectionSettings.UserType;
import etherblocks.engine.states.*;
import etherblocks.engine.states.menu.*;
import etherblocks.mods.Mod;

/**
 *
 * @author Carl
 */
public class ScreenController_StartGame extends GameScreenController{

    public ScreenController_StartGame(){
        
    }
    private Mod[] mods = new Mod[]{
        new Mod("No mod"),
        new etherblocks.mods.shooter.ShooterMod(),
        new etherblocks.mods.survival.SurvivalMod()
    };

    @Override
    protected void initialize(){
        super.initialize();
        Screen screen = nifty.getScreen("startGame");
        Element modSelection = screen.findElementByName("modSelection");
        for(int i=0;i<mods.length;i++){
            final int index = i;
            final Mod mod = mods[i];
            final float y = (45.75f + (i * 4.5f));
            new RadioButtonBuilder(){{
                id("mod_" + index);
                x("6%");
                y(y + "%");
                group("mods");
            }}.build(nifty, screen, modSelection);
            new TextBuilder(){{
                x("8.5%");
                y(y + "%");
                text(mod.getTitle());
                font("Interface/Fonts/Default.fnt");
            }}.build(nifty, screen, modSelection);
        }
    }
    
    public void hostServer(){
        int port = Integer.parseInt(getTextField("hostServerPort").getText());
        startGame(new NetworkConnectionSettings(UserType.SERVER, port));
    }
    
    public void connectToServer(){
        int port = Integer.parseInt(getTextField("connectServerPort").getText());
        NetworkConnectionSettings connectionSettings = new NetworkConnectionSettings(UserType.CLIENT, port);
        connectionSettings.setHost(getTextField("connectServerIP").getText());
        startGame(connectionSettings);
    }
    
    private void startGame(NetworkConnectionSettings connectionSettings){
        Mod mod = getSelectedMod();
        mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(StartMenuAppState.class));
        mainApplication.getStateManager().attach(new ModAppState(mod));
        mainApplication.getStateManager().attach(new NetworkAppState(connectionSettings));
        mainApplication.getStateManager().attach(new BulletAppState());
    }
    
    private Mod getSelectedMod(){
        for(int i=0;i<mods.length;i++){
            RadioButton radioButton = getRadioButton("mod_" + i);
            if(radioButton.isActivated()){
                return mods[i];
            }
        }
        return null;
    }
    
    public void back(){
        goToScreen("start");
    }
}
